﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Acccount : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

        //解決證驗的錯誤
        UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
        //將年月日加入DrowDownList而且加入閨月判斷
        DateTime tnow = DateTime.Now;//現在時間 
        ArrayList AlYear = new ArrayList();
        int i;
        for (i = 1900; i <= 2010; i++)
            AlYear.Add(i);
        ArrayList AlMonth = new ArrayList();
        for (i = 1; i <= 12; i++)
            AlMonth.Add(i);
        if (!this.IsPostBack)
        {
            DropDownList1.DataSource = AlYear;
            DropDownList1.DataBind();//綁定年 
            //選擇當前年 
            DropDownList1.SelectedValue = tnow.Year.ToString();
            DropDownList2.DataSource = AlMonth;
            DropDownList2.DataBind();//綁定月 
            //選擇當前月 
            DropDownList2.SelectedValue = tnow.Month.ToString();
            int year, month;
            year = Int32.Parse(DropDownList1.SelectedValue);
            month = Int32.Parse(DropDownList2.SelectedValue);
            BindDays(year, month);//綁定天 
            //選擇當前日期 
            DropDownList3.SelectedValue = tnow.Day.ToString();
        }
    }


    //判斷閏年 
    private bool CheckLeap(int year)
    {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            return true;
        else return false;
    }
    //綁定每月的天數 
    private void BindDays(int year, int month)
    {
        int i;
        ArrayList AlDay = new ArrayList();

        switch (month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                for (i = 1; i <= 31; i++)
                    AlDay.Add(i);
                break;
            case 2:
                if (CheckLeap(year))
                {
                    for (i = 1; i <= 29; i++)
                        AlDay.Add(i);
                }
                else
                {
                    for (i = 1; i <= 28; i++)
                        AlDay.Add(i);
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                for (i = 1; i <= 30; i++)
                    AlDay.Add(i);
                break;
        }
        DropDownList3.DataSource = AlDay;
        DropDownList3.DataBind();
    }



    //選擇年 
    private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        int year, month;
        year = Int32.Parse(DropDownList1.SelectedValue);
        month = Int32.Parse(DropDownList2.SelectedValue);
        BindDays(year, month);
    }
    //選擇月 


    private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        int year, month;
        year = Int32.Parse(DropDownList1.SelectedValue);
        month = Int32.Parse(DropDownList2.SelectedValue);
        BindDays(year, month);
    } 




    protected void Button1_Click(object sender, EventArgs e)
    {
        //連接資料庫
        if (Page.IsValid)
        {
            //Conn.Open();
          
            char sex = 'M';
            if (TextBoxPassword.Text != TextBoxPassword2.Text)
            {
                Lb1_Msg.Text = "密碼與確認密碼不符";
                return;
            }
            SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);

            try
            {
                Random i = new Random(Guid.NewGuid().GetHashCode());
                int random = i.Next(0, 9999999);
                DateTime registrationdate = DateTime.Now;
                bool visible = false;
                string s = "Insert Into [dbo].[user](user_username,user_password,user_password_random,user_first_name,user_last_name,user_birthday,user_sex,user_mobile_phone,user_email,user_visible,user_registration_date) Values(@Username,@Password,@Random,@Firstname,@Lastname,@Birthday,@Sex,@Phone,@Email,@Visible,@RegistrationDate)";

                SqlCommand sqlcmd = new SqlCommand(s, Conn);

                //宣告參數
                sqlcmd.Parameters.Add("@Username", System.Data.SqlDbType.NVarChar);
                sqlcmd.Parameters.Add("@Password", System.Data.SqlDbType.NVarChar);
                sqlcmd.Parameters.Add("@Firstname", System.Data.SqlDbType.NChar);
                sqlcmd.Parameters.Add("@Lastname", System.Data.SqlDbType.NChar);
                sqlcmd.Parameters.Add("@Birthday", System.Data.SqlDbType.DateTime);
                sqlcmd.Parameters.Add("@Random", System.Data.SqlDbType.NVarChar);
                sqlcmd.Parameters.Add("@Sex", System.Data.SqlDbType.NVarChar);
                sqlcmd.Parameters.Add("@Phone", System.Data.SqlDbType.NChar);
                sqlcmd.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar);
                sqlcmd.Parameters.Add("@RegistrationDate", System.Data.SqlDbType.DateTime);
                sqlcmd.Parameters.Add("@Visible", System.Data.SqlDbType.Bit);

                //給參數作傳遞
                sqlcmd.Parameters["@Username"].Value = TextBoxAccont.Text;
                sqlcmd.Parameters["@Password"].Value = TextBoxPassword.Text;
                sqlcmd.Parameters["@Firstname"].Value = TextBoxRealName.Text;
                sqlcmd.Parameters["@Lastname"].Value = TextBoxName.Text;
                sqlcmd.Parameters["@Birthday"].Value = DropDownList1.SelectedValue+'/'+DropDownList2.SelectedValue+'/'+DropDownList3.SelectedValue;
                sqlcmd.Parameters["@Random"].Value = random;
                sqlcmd.Parameters["@Phone"].Value = TextBoxPhone.Text;
                sqlcmd.Parameters["@Email"].Value = TextBoxEmail.Text;
                sqlcmd.Parameters["@RegistrationDate"].Value = registrationdate;
                sqlcmd.Parameters["@Visible"].Value = visible;

                if (RB_Sex.Checked)
                {
                    sex = 'F';
                }
                sqlcmd.Parameters["@Sex"].Value = sex;
                sqlcmd.Connection.Open();

                try
                {
                    sqlcmd.ExecuteNonQuery();
                    this.Response.Redirect("TEST.aspx");
                }
                catch
                {
                    Lb1_Msg.Text = "錯誤";

                }


            }
            finally
            {
                Conn.Close();
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBoxAccont.Text = "";
       
        TextBoxEmail.Text = "";
        TextBoxName.Text = "";
        TextBoxPassword.Text = "";
        TextBoxPassword2.Text = "";
        TextBoxRealName.Text = "";
        TextBoxPhone.Text = "";
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("https://www.google.com.tw/");
    }
}